from rest_framework import serializers
from .models import *

# 优惠券
class CouponSer(serializers.ModelSerializer):
    coupon_type_name = serializers.SerializerMethodField(read_only=True)
    good_name = serializers.SerializerMethodField(read_only=True)

    def get_coupon_type_name(self,obj):
        return obj.coupon_type.type_name

    def get_good_name(self,obj):
        if obj.good:
            return obj.good.name
        else:
            return ''

    class Meta:
        model = Coupon
        fields = '__all__'
        read_only_fields = [id,]


# 优惠券类型
class CouponTypeSer(serializers.ModelSerializer):
    class Meta:
        model = CouponType
        fields = '__all__'
        read_only_fields = [id,]